import _ from 'lodash';

function computedFun(name, inOpts) {
  let optName = _.lowerFirst(name);
  return {
    get() {
      let opts = this;
      return opts[optName];
    },
    set(val) {
      let opts = this;
      if (_.hasIn(opts, optName)) opts[optName] = val;
      // else Vue.set(opts, optName, val)
    },
  };
}
/**
 * opts : Array
 * [
 *  "","","", // 字符串默认从this.props中取
 *  {"":true, "": false} // 对象key为转换的属性，true表示从props中取，反之this中取
 *  ["",""] // 二层数字固定从this找
 * ]
 */
export default function MakePublicProp(opts) {
  return _.transform(
    opts || [],
    (result, value, key) => {
      let name = value;
      if (_.isString(value)) {
        result[name] = computedFun(name, true);
      } else if (_.isArray(value)) {
        _.each(value, name => {
          result[name] = computedFun(name, false);
        });
      } else if (_.isObject(value)) {
        _.each(value, (inOpts, name) => {
          result[name] = computedFun(name, inOpts);
        });
      }
    },
    {},
  );
}
